package com.itheima.test6;

import org.junit.Before;
import org.junit.Test;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @Author:gy
 * @Date: 2025/09/06 11:46
 * @Description:
 */
public class Demo1 {
    Class clazz;
    @Before
    public void getClazz() throws Exception {
        clazz = Class.forName("com.itheima.test6.People");
    }
    //返回本类的单个方法  不包括私有
    @Test
    public void method3() throws Exception {
        Method show4 = clazz.getMethod("show4", String.class);
        Object o = clazz.newInstance();
        Object zhangsan = show4.invoke(o, "zhangsan");
        System.out.println(zhangsan);
    }
    //获取单个的方法 包括公共和私有的都可以
    @Test
    public void method4() throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Method show = clazz.getDeclaredMethod("show");
        Object o = clazz.newInstance();
        show.setAccessible(true);
        show.invoke(o);
    }
}
